package wiki.zimo.servlet.reception;

import wiki.zimo.constant.Constant;
import wiki.zimo.dao.ProductDao;
import wiki.zimo.domain.Cart;
import wiki.zimo.domain.CartItem;
import wiki.zimo.domain.Product;
import wiki.zimo.servlet.BaseServlet;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @version v1.0
 * @auther: 子墨
 * @datetime: 2018/11/4 21:34
 * @description: 处理购物车页的Servlet
 */
@WebServlet(value = "/cart")
public class CartServlet extends BaseServlet {

    ProductDao dao = new ProductDao();

    /**
     * @auther: 子墨
     * @date: 2018/11/4 21:36
     * @describe: 购物车页
     * @param: [request, response]
     * @return: java.lang.String
     * @version v1.0
     */
    public String cartPage(HttpServletRequest request, HttpServletResponse response) {
        return Constant.URL_CART;
    }

    /**
     * @auther: 子墨
     * @date: 2018/11/4 22:40
     * @describe: 添加商品到购物车接口
     * @param: [request, response]
     * @return: java.lang.String
     * @version v1.0
     */
    public String addProductToCart(HttpServletRequest request, HttpServletResponse response) throws Exception {
        int count = Integer.parseInt(request.getParameter("count"));
        String pid = request.getParameter("pid");

        Product product = dao.findOneById(pid);
        CartItem item = new CartItem(product, count);

        Cart cart = getCartFromSession(request.getSession());
        cart.addCartItemToCart(item);

        // 此处需注意，不能使用请求转发，否则刷新商品会自动增加数目
        return Constant.REDIRECT + Constant.URL_CART;
    }

    /**
     * @auther: 子墨
     * @date: 2018/11/4 22:52
     * @describe: 从购物车中移除商品接口
     * @param: [request, response]
     * @return: java.lang.String
     * @version v1.0
     */
    public String removeProductFromCart(HttpServletRequest request, HttpServletResponse response) {
        String pid = request.getParameter("pid");
        Cart cart = getCartFromSession(request.getSession());
        if (cart != null) {
            cart.removeProductFromCart(pid);
        }
        return Constant.URL_CART;
    }

    /**
     * @auther: 子墨
     * @date: 2018/11/4 22:54
     * @describe: 清空购物车接口
     * @param: [request, response]
     * @return: java.lang.String
     * @version v1.0
     */
    public String removeAllFromCart(HttpServletRequest request, HttpServletResponse response) {
        Cart cart = getCartFromSession(request.getSession());
        if (cart != null) {
            cart.clearCart();
        }
        return Constant.URL_CART;
    }

    /**
     * @auther: 子墨
     * @date: 2018/11/4 22:43
     * @describe: 从当前会话中获取购物车
     * @param: [session]
     * @return: wiki.zimo.domain.Cart
     * @version v1.0
     */
    private Cart getCartFromSession(HttpSession session) {
        Cart cart = (Cart) session.getAttribute(Constant.CART);
        if (cart == null) {
            cart = new Cart();
            session.setAttribute(Constant.CART, cart);
        }
        return cart;
    }
}
